
#include <BAudio>
#include "member_BAudio.h"

using namespace BWE;

#define member					(*(member_BAudio*)_ptr)
#define member_allocate()		_ptr = new member_BAudio(this)
#define member_release()		delete (member_BAudio*)_ptr

BAudio::BAudio(const BString& name) : BObject(name)
{
	member_allocate();
}
BAudio::~BAudio()
{
	member_release();
}

bool BAudio::setFormat(Format format)
{
	if (member.format != format)
	{
		switch (format)
		{
		case Format_Char:
		case Format_UChar:
			member.formatLen = 1;
			break;
		case Format_Short:
		case Format_UShort:
			member.formatLen = 2;
			break;
		case Format_Int:
		case Format_UInt:
			member.formatLen = 4;
			break;
		default:
			return false;
		}
		member.format = format;
		member.dirty = true;
	}
	return true;
}
Format BAudio::format() const
{
	return member.format;
}

bool BAudio::setChannelCount(int channelCount)
{
	if (member.channelCount != channelCount)
	{
		member.channelCount = channelCount;
		member.dirty = true;
	}
	return true;
}
int BAudio::channelCount() const
{
	return member.channelCount;
}

bool BAudio::setSampleRate(int sampleRate)
{
	if (member.sampleRate != sampleRate)
	{
		member.sampleRate = sampleRate;
		member.dirty = true;
	}
	return true;
}
int BAudio::sampleRate() const
{
	return member.sampleRate;
}

void BAudio::setBytes(BByteArray* bytes)
{
	if (member.bytes != bytes)
	{
		member.bytes = bytes;
	}
}
BByteArray* BAudio::bytes() const
{
	return member.bytes;
}

int BAudio::duration() const
{
	int bytesPerSecond = member.channelCount * member.formatLen * member.sampleRate;
	int d = member.bytes->size() * 1000 / bytesPerSecond;
	return d;
}
